<?php

namespace app\admin\controller;

use app\admin\validate\LoginValidate;
use app\domain\admin\LoginDomain;
use think\Exception;
use think\response\Json;

class Login extends Common
{
    public function login(LoginDomain $domain): Json
    {
        try {
            $params = request()->param();
            $validate = validate(LoginValidate::class);
            if (!$validate->scene('login')->check($params)) {
                throw new Exception($validate->getError());
            }

            $result = $domain->login($params);
            return jsonReturn($result ? 0 : 1, $domain->message, $result);
        } catch (\Exception $e) {
            return jsonReturn(1, $e->getMessage());
        }
    }

    public function getUserInfo(LoginDomain $domain): Json
    {
        try {
            $result = $domain->getUserInfo();
            return jsonReturn($result ? 0 : 1, $domain->message, $result);
        } catch (\Exception $e) {
            return jsonReturn(1, $e->getMessage());
        }
    }
}